from sqlite3 import Time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager




from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import cv2
import numpy as np
import time
import random
from io import BytesIO
from PIL import Image


def get_slider_distance(driver):
    """获取滑块需要移动的距离"""
    # 获取验证码图片
    bg_element = driver.find_element_by_css_selector(".geetest_canvas_bg")
    slice_element = driver.find_element_by_css_selector(".geetest_canvas_slice")

    # 截图并转换为OpenCV格式
    bg_screenshot = bg_element.screenshot_as_png
    slice_screenshot = slice_element.screenshot_as_png

    bg_cv = cv2.imdecode(np.frombuffer(bg_screenshot, np.uint8), cv2.IMREAD_COLOR)
    slice_cv = cv2.imdecode(np.frombuffer(slice_screenshot, np.uint8), cv2.IMREAD_COLOR)

    # 灰度化
    bg_gray = cv2.cvtColor(bg_cv, cv2.COLOR_BGR2GRAY)
    slice_gray = cv2.cvtColor(slice_cv, cv2.COLOR_BGR2GRAY)

    # 边缘检测
    bg_edge = cv2.Canny(bg_gray, 100, 200)
    slice_edge = cv2.Canny(slice_gray, 100, 200)

    # 模板匹配
    result = cv2.matchTemplate(bg_edge, slice_edge, cv2.TM_CCOEFF_NORMED)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)

    return max_loc[0]  # 返回缺口x坐标


def move_slider(driver, slider_element, distance):
    """模拟人类拖动滑块"""
    action = ActionChains(driver)
    action.click_and_hold(slider_element).perform()

    # 模拟人类拖动轨迹(先快后慢)
    total_tracks = []
    current_offset = 0

    # 初始快速移动(约40%距离)
    fast_distance = int(distance * 0.4)
    while current_offset < fast_distance:
        move = random.randint(3, 8)
        action.move_by_offset(move, random.randint(-2, 2)).perform()
        current_offset += move
        total_tracks.append(move)
        time.sleep(random.uniform(0.01, 0.05))

    # 中间减速移动(约30%距离)
    mid_distance = int(distance * 0.7)
    while current_offset < mid_distance:
        move = random.randint(2, 5)
        action.move_by_offset(move, random.randint(-2, 2)).perform()
        current_offset += move
        total_tracks.append(move)
        time.sleep(random.uniform(0.05, 0.1))

    # 最后缓慢移动(剩余距离)
    while current_offset < distance:
        move = random.randint(1, 3)
        if current_offset + move > distance:
            move = distance - current_offset
        action.move_by_offset(move, random.randint(-2, 2)).perform()
        current_offset += move
        total_tracks.append(move)
        time.sleep(random.uniform(0.1, 0.3))

    # 小幅回拉模拟人类操作
    back_move = random.randint(-3, -1)
    action.move_by_offset(back_move, 0).perform()
    time.sleep(0.2)

    action.release().perform()
    time.sleep(1)



ChromeIns = ChromeDriverManager().install()
driver = webdriver.Chrome(service=Service(ChromeIns))

driver.implicitly_wait(40)
driver.maximize_window()

driver.get("https://www.mudaiba.com/")

driver.find_element(By.CSS_SELECTOR,'#nav > ul:nth-child(2) > li:nth-child(2) > a').click() #登录点击


driver.find_element(By.CSS_SELECTOR,'#captcha > div > div.geetest_holder_d5423738.geetest_holder > div.geetest_btn_click_d5423738.geetest_btn_click').click() #人机校验

#----------------------------------------

slider = driver.find_element(By.CSS_SELECTOR,
    "#captcha > div > div.geetest_holder_d5423738.geetest_holder > " +
    "div.geetest_box_wrap_d5423738.geetest_box_wrap > " +
    "div.geetest_box_d5423738.geetest_box > " +
    "div.geetest_container_d5423738.geetest_container > " +
    "div > div > div.geetest_slider_d5423738.geetest_slider > " +
    "div > div.geetest_btn_d5423738.geetest_btn"
)

# 获取需要移动的距离
distance = get_slider_distance(driver)
print(f"需要移动的距离: {distance}像素")

# 移动滑块
move_slider(driver, slider, distance)

time.sleep(2)  # 等待验证结果

#----------------------------------------

driver.find_element(By.CSS_SELECTOR,'#submit').click()#点击登录
driver.find_element(By.CSS_SELECTOR,'#m_sign').click()#签到点击

Time.sleep(10)

driver.quit()